package com.polaris.live.im.utils

import com.polaris.live.im.sfs.bean.pusher.PusherNoticeMessage
import com.polaris.live.common.util.JsonUtils
import com.smartfoxserver.v2.entities.data.ISFSObject

/**
 * Impusher 的  ISFSObject解析
 *
 * @author Created by 半仙 on 2023/8/31/031 16:51
 */
object AdminMessageUtil {

    /**
     * 解析pusher的通用消息
     */
    fun parseNoticeMessage(sfsObject: ISFSObject): PusherNoticeMessage {
        return PusherNoticeMessage(
            msgId = sfsObject.getUtfString("msgId") ?: throw NullPointerException("NoticeMessage msgId is null"),
            type = sfsObject.getLong("type") ?: throw NullPointerException("NoticeMessage type is null"),
            content = sfsObject.getUtfString("content") ?: throw NullPointerException("NoticeMessage content is null"),
            tranContent = sfsObject.getUtfString("tranContent") ?: null,
            needAck = sfsObject.getBool("needAck") ?: null
        )
    }

    /**
     * 解析为通知内容对象
     */
    inline fun <reified T> parseContentMessage(sfsObject: ISFSObject): T? {
        val data = parseNoticeMessage(sfsObject)
        return JsonUtils.fromJson<T>(data.content)
    }
}